/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javazabbix;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 *
 * @author max
 */
public class JavaZabbix {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        
        // TODO code application logic here
        String server = "172.16.125.67"; //args[0];
        String user = "enterprisedb"; //args[1];
        String pass = "Ufufhby1961"; //args[2];
        String db = "minsport"; //args[3];
        String sql1 = "SELECT card_id, file_name FROM card WHERE file_store_url IS NOT NULL group by card_id, file_name";
        String sql2 = "SELECT card_id, file_name FROM card_version WHERE file_store_url IS NOT NULL group by card_id, file_name";
        Integer count1 = 0;
        Integer count2 = 0;
        
        //подключение к БД 
        Class.forName("org.postgresql.Driver");
        String url = "jdbc:postgresql://"+server+"/"+db;
        Connection connection = DriverManager.getConnection(url, user, pass);
        Statement stmt1 = connection.createStatement();
        Statement stmt2 = connection.createStatement();
        ResultSet rs1 = stmt1.executeQuery(sql1);
        ResultSet rs2 = stmt2.executeQuery(sql2);
        
        //заполнение листов данными
        List<String> nameList1 = new ArrayList<String>();
        while (rs1.next()) {
             nameList1.add(rs1.getString("card_id"));
             nameList1.add(rs1.getString("file_name"));
             count1++;
        }

        List<String> nameList2 = new ArrayList<String>();
        while (rs2.next()) {
            nameList2.add(rs2.getString("card_id"));
            nameList2.add(rs2.getString("file_name"));
            count2++;
        }
        
        //вычсление общего количества файлов и файлов удовлетворяющих условию
        Integer summ = count1 + count2;
        Boolean check;
        for (int y = 0; y < nameList1.size()-1; y++) {
            check = false;
            for (int i = 0; i < nameList2.size()-1; i++) {
                if (nameList1.get(y).equals(nameList2.get(i)) && nameList1.get(y+1).equals(nameList2.get(i+1))) {
                    check = true;
                }
            }
            if (check) {
                summ = summ - 1;
            }
        }
        
        System.out.println(summ);
        
        rs1.close();
        rs2.close();
        stmt1.close();
        stmt2.close();
        connection.close();
        
    }
}
